package com.kjc.demo.prometheus.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("proxy")
@Slf4j
public class RainDayController {
	
	@Autowired
	private RestTemplate restTemplate;
	
	private static final String RAIN_DAY_URL ="http://10.5.230.53:30072/api/v1/query_range?query=100 * (1 - sum(node_memory_MemAvailable_bytes) / sum(node_memory_MemTotal_bytes))&start=1657765147.756&end=1657768747.756&step=14&_=1657768390924";
	//prometheus请求前缀
	private static final String URL_PREFIX = "http://10.5.230.53:30072/api/v1";
	
	//prometheus 节点请求前缀
	private static final String NODE_URL_PREFIX = "https://10.5.230.61:30073/graph/api/datasources/proxy/1/api/v1";
	
	//cpu请求后缀
//	private static final String CPU_SUBFIX = "/query_range?query=1-(avg(irate(node_cpu_seconds_total{mode=\"idle\"}[5m])))&start=1657781648.372&end=1657785248.372&step=14&_=1657784674325";
	private static final String CPU_SUBFIX = "/query_range?query=1-(avg(irate(node_cpu_seconds_total{mode}[5m])))&start=1657781648.372&end=1657785248.372&step=14&_=1657784674325";
	
	//内存请求后缀
	private static final String MEMORY_SUBFIX = "/query_range?query=100*(1-sum(node_memory_MemAvailable_bytes)/sum(node_memory_MemTotal_bytes))&start=1657782884.625&end=1657786484.625&step=14&_=1657784674327";	
	
	//磁盘请求后缀
//	private static final String DISK_SUBFIX = "/query_range?query=(sum(node_filesystem_size_bytes{device!~\"rootfs|HarddiskVolume.+\",node=~\"^.*$\"})-sum(node_filesystem_free_bytes{device!~\"rootfs|HarddiskVolume.+\",node=~\"^.*$\"}))/sum(node_filesystem_size_bytes{device!~\"rootfs|HarddiskVolume.+\",node=~\"^.*$\"})*100&start=1657783527.416&end=1657787127.416&step=14&_=1657784674329";
	private static final String DISK_SUBFIX = "/query_range?query=(sum(node_filesystem_size_bytes{device})-sum(node_filesystem_free_bytes{file}))/sum(node_filesystem_size_bytes{filesize})*100&start=1657783527.416&end=1657787127.416&step=14&_=1657784674329";
	
	//节点cpu核数
	private static final String NODE_CPU_CORES = "/query?query=label_replace(sum(instance:node_num_cpu:sum)by(instance),\"instance\",\"$1\",\"instance\",\"(.*):(.*)\")&time=1657705864";
	
	//节点CPU负载
	private static final String NODE_CPU_LOAD = "/query_range?query=label_replace(sum(container_fs_usage_bytes)by(instance)/sum(container_fs_limit_bytes )by(instance),\"instance\",\"$1\",\"instance\",\"(.*):(.*)\")&start=1657785404.284&end=1657789004.284&step=14&_=1657768390926";
	
	//单个节点cpu负载
	private static final String SINGLE_NODE_CPU_LOAD = "/query_range?query=1-(avg(irate({name}[1m])))&start=1657786108.331&end=1657789708.331&step=14&_=1657768390928";
	
	//节点内存
//	private static final String NODE_MEMORY = "/query_range?query=(node_memory_MemTotal_bytes{instance=\"10.5.230.53:12921\"}-node_memory_MemFree_bytes{instance=\"10.5.230.53:12921\"}-node_memory_Buffers_bytes{instance=\"10.5.230.53:12921\"}-node_memory_Cached_bytes{instance=\"10.5.230.53:12921\"})/(node_memory_MemTotal_bytes{instance=\"10.5.230.53:12921\"})&start=1657786730.569&end=1657790330.569&step=14&_=1657768390930";
	private static final String NODE_MEMORY = "/query_range?query=(node_memory_MemTotal_bytes{instance}-node_memory_MemFree_bytes{instance}-node_memory_Buffers_bytes{instance}-node_memory_Cached_bytes{instance})/(node_memory_MemTotal_bytes{instance})&start=1657786730.569&end=1657790330.569&step=14&_=1657768390930";
	
	//节点磁盘使用率
	private static final String NODE_DISK = "/query_range?query=1-({fileavail}/{filesize})&start=1657787693.248&end=1657791293.248&step=14&_=1657768390932";
	
	//节点磁盘容量
	private static final String NODE_DISK_CAPACITY = "/query_range?query={fileavail}&start=1657788536.269&end=1657792136.269&step=14&_=1657768390934";
	
	//各节点磁盘容量
	private static final String ALL_NODE_DISK_CAPACITY = "/query_range?query={fileavail}&start=1657788938.396&end=1657792538.396&step=14&_=1657768390936";
	
	@GetMapping("getCpuHistory")
	public Object getCpuHistory() {
		String url = URL_PREFIX + CPU_SUBFIX;
		log.info("CPU请求URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("mode", "{mode=\"idle\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class,paramMap);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getMemory")
	public Object getMemory() {
		String url = URL_PREFIX + MEMORY_SUBFIX;
		log.info("内存请求URL{}", url);
		JSONObject json = restTemplate.getForObject(url, JSONObject.class);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getDisk")
	public Object getDisk() {
		String url = URL_PREFIX + DISK_SUBFIX;
		log.info("磁盘请求URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("device", "{device!~\"rootfs|HarddiskVolume.+\",node=~\"^.*$\"}");
		paramMap.put("file", "{device!~\"rootfs|HarddiskVolume.+\",node=~\"^.*$\"}");
		paramMap.put("filesize", "{device!~\"rootfs|HarddiskVolume.+\",node=~\"^.*$\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class, paramMap);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getNodeCpuCores")
	public Object getNodeCpuCores(String node) {
		String url = URL_PREFIX + NODE_CPU_CORES;
		log.info("节点cpu核数 URL{}", url);
		JSONObject json = restTemplate.getForObject(url, JSONObject.class);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getNodeCpuload")
	public Object getNodeCpuload(String node) {
		String url = URL_PREFIX + NODE_CPU_LOAD;
		log.info("节点cpu负载 URL{}", url);
		JSONObject json = restTemplate.getForObject(url, JSONObject.class);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getSingleNodeCpuload")
	public Object getSingleNodeCpuload(String node) {
		String url = URL_PREFIX + SINGLE_NODE_CPU_LOAD;
		log.info("单个节点cpu负载 URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("name", "{__name__=~\"node_cpu(|_seconds_total)\",mode=\"idle\",instance=\"10.5.230.53:12921\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class, paramMap);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getNodeMemory")
	public Object getNodeMemory(String node) {
		String url = URL_PREFIX + NODE_MEMORY;
		log.info("单个节点内存负载 URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("instance", "{instance=\"10.5.230.53:12921\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class, paramMap);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getNodeDiskUserate")
	public Object getNodeDisk(String node) {
		String url = URL_PREFIX + NODE_DISK;
		log.info("单个节点磁盘 URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("fileavail", "{__name__=~\"node_filesystem_avail(_bytes|)\",instance=\"10.5.230.53:12921\",fstype=~\"ext.*|xfs\",mountpoint=~\"^/.*\",job=\"node-exporter\"}");
		paramMap.put("filesize", "{__name__=~\"node_filesystem_size(_bytes|)\",instance=\"10.5.230.53:12921\",fstype=~\"ext.*|xfs\",mountpoint=~\"^/.*\",job=\"node-exporter\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class, paramMap);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getNodeDiskCapacity")
	public Object getNodeDiskCapacity(String node) {
		String url = URL_PREFIX + NODE_DISK_CAPACITY;
		log.info("单个节点磁盘容量URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("fileavail", "{__name__=~\"node_filesystem_avail(_bytes|)\",instance=\"10.5.230.53:12921\",fstype=~\"ext.*|xfs\",mountpoint=~\"^/.*\",job=\"node-exporter\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class, paramMap);
		log.info("{}",json);
		return json;
	}
	
	@GetMapping("getAllNodeDiskCapacity")
	public Object getAllNodeDiskCapacity(String node) {
		String url = URL_PREFIX + ALL_NODE_DISK_CAPACITY;
		log.info("所有节点磁盘容量 URL{}", url);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("fileavail", "{__name__=~\"node_filesystem_size(_bytes|)\",instance=\"10.5.230.53:12921\",fstype=~\"ext.*|xfs\",mountpoint=~\"^/.*\",job=\"node-exporter\"}");
		JSONObject json = restTemplate.getForObject(url, JSONObject.class, paramMap);
		log.info("{}",json);
		return json;
	}
	
	
	@GetMapping("getMonitor")
	public Object getInfo(String date) {
		String url = RAIN_DAY_URL;
		JSONObject json = restTemplate.getForObject(url, JSONObject.class);
		log.info("{}",json);
		return json;
	}
	
}
